use std::env;
use std::fs;
use std::process;

mod lexer;

use lexer::Lexer;

fn main() {
    let args: Vec<String> = env::args().collect();
    
    if args.len() != 2 {
        eprintln!("用法: {} <文件名>", args[0]);
        process::exit(1);
    }
    
    let filename = &args[1];
    

    let content = match fs::read_to_string(filename) {
        Ok(content) => content,
        Err(e) => {
            eprintln!("Cant read file!!!! '{}': {}", filename, e);
            process::exit(1);
        }
    };
    
    let mut lexer = match Lexer::new(&content) {
        Ok(lexer) => lexer,
        Err(e) => {
            eprintln!("Lexer_error!!!!!: {}", e);
            process::exit(1);
        }
    };
    
    if lexer.has_errors() {
        for error in lexer.errors() {
            eprintln!("Error type A at Line {}: {}", error.line, error.message);
        }
        process::exit(1);
    }
    
    while let Some(token_with_pos) = lexer.next() {
        let token_str = token_with_pos.token.to_string();
        eprintln!("{} at Line {}.", token_str, token_with_pos.line);
    }
    
}
